package array

import "fmt"

// 输入一个矩阵，按照从外到里以顺时针的顺序依次打印出每一个数字
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// 13 14 15 16
// 依次打印出数字：1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

func PrintMatrixInCircle(a [][]int, columns, rows, start int) {
	var endX = columns - 1 - start
	var endY = rows - 1 - start

	// 从左到右打印一行
	for i := start; i <= endX; i++ {
		number := a[start][i]
		fmt.Print(" ")
		fmt.Print(number)
	}
	// 从上到下打印一列
	if start < endY {
		for i := start + 1; i <= endY; i++ {
			number := a[i][endX]
			fmt.Print(" ")
			fmt.Print(number)
		}
	}
	// 从右到左打印一行
	if start < endX && start < endY {
		for i := endX - 1; i >= start; i-- {
			number := a[endY][i]
			fmt.Print(" ")
			fmt.Print(number)
		}
	}
	// 从下到上打印一列
	if start < endX && start < endY-1 {
		for i := endY - 1; i >= start+1; i-- {
			number := a[i][start]
			fmt.Print(" ")
			fmt.Print(number)
		}
	}
}
